/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.parts.LoggedData.panels;

import java.awt.Component;
import researchgrants.parts.LoggedData.*;
import researchgrants.parts.LoggedData.panels.events.SaveValidationEvent;
import researchgrants.parts.panels.Panels;

/**
 * An abstract displayer for a perticular HistoricallyLoggedData.
 * To actually display HistoricallyLoggedData, a decendant of LoggedDataPanels should be used.
 * @author DOStudent1
 */
public abstract class LoggedDataPanels extends Panels {

      
    // Gets the view and edit panels
    
    public abstract Component getEditComponent();

    public abstract Component getViewComponent();
    
  
    // Support a default value, when no logged data is provided
    private String defaultValue="";
     
    public void setDefaultValue(String defaultValue) {
        this.defaultValue=defaultValue;
    }
    
    protected String getDefaultValue() {
        return(defaultValue);
    }
    
    // if data is null, cannot display it.
    // if data is not null, can edit it (used for inserting new data)
    private HistoricallyLoggedData data;
    private boolean hasData=false;

    
    // this function helps the ViewEditPanels to get the actual data
    public HistoricallyLoggedData getData() {
        return(data);
    }
    
    // an internal function to set the data to be used by the data panels
    public void setData(HistoricallyLoggedData data) {
        hasData=true;
        this.data=data;
    }
    
    protected boolean hasDataInitialized() {
        return(hasData);
    }

    protected void validateDataToDisplay() {
        if (!hasDataInitialized()) {
            throw new RuntimeException("Data was not initialized");
        }
        if (!isParentInitialized()){
            throw new RuntimeException("Parent was not initialized");
        }
    }
    
    // If the Edit panel is being used to change the data, what is the new value 
    // This is used to actually save the data (not part of the panel, but of HistoricallyLoggedData)
    abstract public LogableObject getEdittedValue();

    // defines how to save this type of data:
    // for HistoricallyLoggedData - using the inner getEdittedValue and setNewValue or createNew
    public void saveData()  {
        if (data==null) {
            // saving data for the first time
            HistoricallyLoggedData newData=HistoricallyLoggedData.createNew(getEdittedValue());
            setData(newData);
            notifyAllSaveValidationListeners(new SaveValidationEvent(this));
        } else {
            LogableObject edittedValue = getEdittedValue();
            // TODO: saving only when the data had changed
            // saving data for an existing HistoricallyLoggedData
            // should be called only if data is available (when the user is allowed to edit)
            data.setNewValue(edittedValue);
            notifyAllSaveValidationListeners(new SaveValidationEvent(this));
        }
    }

}
